/**
 * Project Name:TEST_LOG4J_SOCKET
 * File Name:LOlogger2.java
 * Package Name:com.example.utility
 * Date:2014年1月22日下午3:09:36
 * Copyright (c) 2014, ShangHai Leadon IOT Technology Co.,Ltd.  All Rights Reserved.
 */

package com.csjbot.weedle_auto.util

import org.slf4j.LoggerFactory
import java.io.Serializable

/**
 * ClassName:LOlogger2 <br></br>
 * 用法：在类中定义 <br></br>
 * private static final Csjlogger loLogger = new Csjlogger(xxxx.class); <br></br>
 * loLogger.info(xxxxx); Date: 2014年1月22日 下午3:09:36 <br></br>
 *
 * @author "浦耀宗"
 * @see
 */

class Csjlogger : Serializable {

    private fun getSimpleClassName(name: String): String {
        val lastIndex = name.lastIndexOf(".")
        return name.substring(lastIndex + 1)
    }

    companion object {
        /**
         * serialVersionUID:TODO(用一句话描述这个变量表示什么).
         */
        private const val serialVersionUID = 1245549555563181056L

        private val DBL_LOGGER = LoggerFactory.getLogger("DBL")
        private val DEFAULT_LOGGER = LoggerFactory.getLogger("csjbot_squirtle")

        // =============== debug================/
        fun debug(msg: String) {
            DEFAULT_LOGGER.debug(msg + lineOut)
        }

        fun debug(msg: String, t: Throwable) {
            DEFAULT_LOGGER.debug(msg + lineOut, t)
        }

        fun debug(format: String, vararg arguments: Any) {
            DEFAULT_LOGGER.debug(format + lineOut, arguments)
        }

        // =============== info================/
        // =============== info================/
        fun info(msg: String) {
            DEFAULT_LOGGER.info(msg + lineOut)
        }

        fun info(msg: String, t: Throwable) {
            DEFAULT_LOGGER.info(msg + lineOut, t)
        }

        fun info(format: String, vararg arguments: ByteArray) {
            DEFAULT_LOGGER.info(format + lineOut, arguments)
        }

        // =============== warn================/
        // =============== warn================/
        fun warn(msg: String) {
            DEFAULT_LOGGER.warn(msg + lineOut)
        }

        fun warn(msg: String, t: Throwable) {
            DEFAULT_LOGGER.warn(msg + lineOut, t)
        }

        fun warn(format: String, vararg arguments: Any) {
            DEFAULT_LOGGER.warn(format + lineOut, arguments)
        }

        // =============== error================/
        // =============== error================/
        fun error(msg: String) {
            DEFAULT_LOGGER.error(msg + lineOut)
        }

        fun error(msg: String, t: Throwable) {
            DEFAULT_LOGGER.error(msg + lineOut, t)
        }

        fun error(format: String, vararg arguments: Any) {
            DEFAULT_LOGGER.error(format + lineOut, arguments)
        }

        // =============== Debug Line================/
        // =============== Debug Line================/
        fun DBL() {
            DBL_LOGGER.trace(" ========== $lineOut ========== ")
        }

        private val lineOut: String
            get() {
                val stackTraceElement = Thread.currentThread()
                        .stackTrace
                var currentIndex = -1
                for (i in stackTraceElement.indices) {
                    if (stackTraceElement[i].methodName.compareTo("getLineOut") == 0) {
                        currentIndex = i + 2
                        break
                    }
                }

                val builder = StringBuilder()
                builder.append(" [")
                        .append(stackTraceElement[currentIndex].methodName)
                        .append("] (")
                        .append(stackTraceElement[currentIndex].fileName)
                        .append(":")
                        .append(stackTraceElement[currentIndex].lineNumber)
                        .append(")")
                return builder.toString()
            }
    }
}
